草庐IT

javascript oop、instanceof 和基类

全部标签

c++ - 结构作为基类 - C/C++ 互操作性

我记得我在某处看到过一些代码,这些代码曾经将结构作为基类,将C++类作为派生类structBase_Struct{}classDerived:Base_Struct{...}重点是指向Base_Struct*的指针从C++文件传递​​到一些C文件,然后这些文件设法使用Base_Struct中的一些函数指针。我的问题是:如果我将Base_Struct*传递给C文件,C代码是否能够完全使用Base_Struct?派生类呢? 最佳答案 IfIpassBase_Struct*toaCfile,willtheCcodebeabletouset

c++ - 使用 typedeffing 模板化基类来简化代码是一种好习惯吗?

最近在处理许多模板化类并从它们派生时,我发现自己“发明”了这个简单的结构。我不确定这是常见做法,还是我在脖子上系了一根绳子。templateclassBase{};templateclassDerived:publicBase{typedefBaseBase;};我发现如果Base它特别有用类有自己的typedefs对于某些类型。例如:templateclassBase{typedefTScalar;typedefMatrixMatrix;};然后很容易将类型“导入”到Derived中.它节省了重新键入模板签名。例如:templateclassDerived:publicBase{ty

c++ - 初始化列表是否适用于基类?

初始化列表是否适用于基类?如果是这样,如何?例如structA{inti;};structB:publicA{doubled;};intmain(){Bb{A(10),3.4};return0;} 最佳答案 标准的第8.5.1节定义了聚合:Anaggregateisanarrayoraclass(Clause9)withnouser-providedconstructors(12.1),nobrace-or-equal-initializersfornon-staticdatamembers(9.2),noprivateorprot

c++ - 如何从非多态虚基类向下转型?

有没有办法在不涉及虚函数的情况下从虚基类向下转型为派生类?下面是一些代码来演示我在说什么:structBase1{intdata;};structBase2{charodd_size[9];};structViBase{doublevalue;};structMostDerived:Base1,Base2,virtualViBase{boolok;};voidfoo(ViBase&v){MostDerived&md=somehow_cast(v);//butHOW?md.ok=true;}intmain(){MostDerivedmd;foo(md);}请注意,该代码仅用于演示。我的真

c++ - 具有基于 protected 基类方法的返回类型的函数

我试图让doSomeMethod()的返回类型与operator()相同在基类中,但如果它被声明为protected,编译器将拒绝带有error:notypenamed'type'in'std::result_of'的代码.如果它是公开的,它就可以工作,但我想知道从那以后我是否也可以让它为protected案例工作。这是重现错误的简单代码。#includeclassbase{protected:intoperator()(){return1;};};classchild:privatebase{autostaticdoSomeMethod()->typenamestd::result_

c++ - 继承和复制构造函数——如何从基类初始化私有(private)字段?

我有2个类,A和B。在A中,我有3个私有(private)字段。在B类中,我想编写一个复制构造函数,并从A类中初始化私有(private)字段。但是,这不起作用:#include#includeusingnamespacestd;classA{private:string*field1;string*field2;string*field3;doublenum1;public:A(string*o,string*n,string*m,doublea=0){field1=newstring(*o);field2=newstring(*n);field3=newstring(*m);num

c++ - 从基类的指针访问派生类的成员

我有一个从另一个派生的类,我使用一个基类指针数组来保存派生类的实例,但是因为数组是基类的,所以我无法使用指针访问属于派生类的成员符号,我是否可以使用简单的命令访问这些成员,或者我是否应该重写我的基类以定义成员并仅在派生类中使用它?例子:classA{public:intfoo;};classB:publicA{public:charbar;};classC:publicA{inttea;};intmain(){A*arr[5];arr[0]=newB;chartest=arr[0]->bar;//visualstudiohighlightsthiswithanerror"classAh

c++ - 为什么编译器在使用 CRTP 时看不到基类的方法

我有以下代码:structIface{virtualintRead()=0;intRead(intx){returnRead()+x;}};templatestructCrtp:publicIface{virtualintRead(){returnstatic_cast(*this).ReadImpl();}//usingIface::Read;};structIfaceImpl:publicCrtp{intReadImpl(){return42;}};intmain(){IfaceImplimpl;impl.Read(24);//compilationerrorIface&iface

c++ - 为什么派生类不能通过指向基类的指针访问其基类的 protected 成员?

这是code:classTestA{protected:inttest=12;public:TestA(){couttest;}~TestB(){}};intmain(){TestA*pTestA=newTestA();TestB*pTestB=newTestB(pTestA);}我正在尝试使用指向TestA类型对象的指针访问protected成员(因此,TestA的实例).TestB也派生自TestA为什么我无法访问它?它只能在我需要它的类(class)“内部”访问吗?不在外部使用指针/直接声明? 最佳答案 当public继承自基

c++ - Const 类型转换空基类

const_cast离开一个空基类并在其上调用非const方法是否是未定义的行为?例如classEmptyBase{public:voidbar(){...}};classSomething:publicEmptyBase{public:voidfoo()const{const_cast(static_cast(*this)).bar();}};我没能在标准(C++14和C++17)中找到相关信息来回答这个问题.. 最佳答案 它本身并不是UB。当您放弃常量并使用获得的泛左值修改最初声明为常量的对象时,您会得到未定义的行为。这是对此的